package htw.list;

// Java-Beispiele
// Prof. Dr. H. G. Folz
// 2001
//
// Testen der Klasse DList, insbesondere des ListIterators
//
 
import java.io.*;
import java.util.*;
import de.htw.saarland.stl.Stdin;

/** Test der ListenKlasse
 */
public class DListTest {
    private final int HAS_NEXT     = 1;
    private final int HAS_PREVIOUS = 2;
    private final int NEXT         = 3;
    private final int NEXT_INDEX   = 4;
    private final int PREVIOUS     = 5;
    private final int PREVIOUS_INDEX = 6;
    private final int LIST_ITERATOR  = 7;
    private final int ENDE      = 99;

    private PrintStream out = System.out;

    public DListTest() {
    }

    public void start() {
        DList l1 = new DList();
        fuellen(l1);
        java.util.ListIterator it = null;
        int auswahl = 0;
        int index = 0;
        String eingabe;
        do {
           try {
              out.println("\nl1: " + l1 + "\n");
              auswahl = auswaehlen();
              out.println("\n");
              switch (auswahl) {
              case HAS_NEXT:
                   out.println("hasNext: " + it.hasNext());
                   break;
              case HAS_PREVIOUS:
                   out.println("hasPrevious: " + it.hasPrevious());
                   break;
              case PREVIOUS:
                   out.println("previous: " + it.previous());
                   break;
              case PREVIOUS_INDEX:
                   out.println("previousIndex: " + it.previousIndex());
                   break;
              case NEXT:
                   out.println("next: " + it.next());
                   break;
              case NEXT_INDEX:
                   out.println("nextIndex: " + it.nextIndex());
                   break;
              case LIST_ITERATOR:
                   out.println("iterator: ");
                   index = Stdin.readlnInt("index =  ");
                   it = l1.iterator(index);
                   break;
              case ENDE : break;
              default   : System.out.println("Falsche Auswahl!");
              }
           } catch (NoSuchElementException e) {
               out.println(e);
           } catch (Exception e) {
               e.printStackTrace();
           }
        } while (auswahl != ENDE);

    }

    void fuellen(DList l) {
        l.addLast("Albert");
        l.addLast("Berta");
        l.addLast("Caesar");
        l.addLast("Dora");
        l.addLast("Emil");
        l.addLast("Fritz");
    }

    public int auswaehlen() {
        out.print(
                        + HAS_NEXT  + ": hasNext / "
                        + HAS_PREVIOUS + ": hasPrevious / "
                        + NEXT  + ": next / "
                        + NEXT_INDEX + ": nextIndex / "
                        + PREVIOUS + ": previous / "
                        + PREVIOUS_INDEX + ": previousIndex /\n"
                        + LIST_ITERATOR + ": listIterator / "
                        + ENDE  + ": beenden -> ");
       return Stdin.readlnInt();
   }


    public static void main(String[] args) {
        try {
            DListTest l = new DListTest();
            l.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
